define(['jquery', 'bootstrap', 'frontend', 'form', 'template'], function ($, undefined, Frontend, Form, Template) {
    var Controller = {
        index: function () {

            function init() {
                var amount = $('.p-status').eq(1).data('amount');
                var sn = $('.p-status').eq(1).data('sn');
                if (Config.payType == 'paytm'){
                        Fast.api.ajax({
                        url: '/index/order/pay',
                        data: {
                            amount: amount,
                            sn: sn,
                        }
                    }, function (data, ret) {
                           console.log(data);
                            console.log(ret);
                            if (data.status){
                                Fast.api.open(data.data.payURL)
                            }else {
                                Toastr.error('pay error');
                            }
                        return false;
                    }, function (data, ret) {
                            console.log(ret)
                            console.log(data)
                        Toastr.error('pay error');
                        return false;
                    });
                }else{
                    if (amount){
                        $('#paymentAmount').html(amount);
                        $('#paymentShow').css('display','block');
                    }
                }

            }
            init();

            $('.hide-popup').on('click', function(){
                $('#paymentShow').css('display','none');
            });

            $('.paymentOn').on('click', function(){
                var amount = $(this).data('amount');
                $('#paymentAmount').html(amount);
                $('#paymentShow').css('display','block');
            });

            $('input[name="payType"]').on('change', function(){
                if ($(this).attr('paytm')){

                }else {

                }
            });
        },
        shipping: function () {
            $('#add_address').on('click', function(){
                $('#add_address-conatiner').css('display','block');
                //Layer.msg('success');
            });

            $('.hide-popup').on('click', function(){
                $('#add_address-conatiner').css('display','none');
            });

            $('#add_address_button').on('click', function(){
                event.preventDefault();
                let name = $('input[name="addressName"]').val();
                let mobile = $('input[name="addressMobile"]').val();
                let area = $('input[name="addressArea"]').val();
                let detail = $('input[name="addressDetail"]').val();

                if (!name || !mobile || !area || !detail){
                    Layer.msg('Please input params');
                    return false;
                }
                Fast.api.ajax({
                    url: "/api/address/add",
                    data: {
                        name: name,
                        mobile: mobile,
                        area: area,
                        detail: detail,
                        is_default: 1,
                    }
                }, function (data, ret) {
                    let html = "<div class=\"col-md-4\"><input type=\"hidden\" name=\"address_id\" value="+ ret.data.address_id + ">\n" +
                        "                            <div class=\"shipping-discount\">\n" +
                        "                                <div class=\"shipping-title\">\n" +
                        "                                    <h3>Shopping Address</h3>\n" +
                        "                                </div>\n" +
                        "                                <p>" + name + " | "  + mobile+"</p>\n" +
                        "                                <div class=\"row\">\n" +
                        "                                    <div class=\"col-lg-12 col-md-12 col-sm-6\">\n" +
                        "                                        <div class=\"level\">\n" +
                        "                                            Country: "+area+"\n" +
                        "                                        </div>\n" +
                        "                                    </div>\n" +
                        "                                    <div class=\"col-lg-12 col-md-12 col-sm-6\">\n" +
                        "                                        <div class=\"level\">\n" +
                        "                                            Detail: "+detail+"\n" +
                        "                                        </div>\n" +
                        "                                    </div>\n" +
                        "                                </div>\n" +
                        "                            </div></div>";
                    $('#add_address-conatiner').css('display','none');
                    $("#addressBox").html(html);
                    Toastr.success('Add Success');
                    return false;
                }, function (data, ret) {
                    Toastr.error(ret.msg);
                    return false;
                });
            });

            $(document).on('click', '#checkout', function () {
                event.preventDefault();
                let address_id = $('input[name="address_id"]').val();
                // let products_id = $('input[name="products_id"]').val();
                // let num = $('input[name="num"]').val();
                let payType = $('input[name="payType"]:checked').val();

                let products_id = [];
                $('input[name="products_id[]"]').each(function (i) {
                    products_id[i] = $(this).val();
                })
                let num = [];
                $('input[name="num[]"]').each(function (i) {
                    num[i] = $(this).val();
                })

                // if ( $('input[name="payType"]:checked').val() == 'paytm'){
                //     Layer.msg('Paytm Not open');
                //     return false;
                // }
                if (!address_id){
                    Layer.msg('Please input Shopping Address');
                    return false;
                }
                if (!num){
                    Layer.msg('Please input number');
                    return false;
                }
                if (!products_id){
                    Layer.msg('Please Select Products');
                    return false;
                }

                Layer.confirm('Confirm Checkout?', {
                    title: 'message',
                    btn: [__('Yes'), __('No')]
                }, function (index) {
                    Layer.close(index);
                    Fast.api.ajax({
                        url: "/api/order/creates",
                        data: {
                            address_id: address_id,
                            products_id: products_id,
                            num: num,
                        }
                    }, function (data, ret) {
                        Toastr.success('Create Success');
                        //todo 跳转 订单页面
                        window.location.href='/index/order/index?payType=' + payType;

                        return false;
                    }, function (data, ret) {
                        Toastr.error(ret.msg);
                        return false;
                    });
                }, function (index) {
                    Layer.close(index);
                });
                return false;
            });
        }
    };
    return Controller;
});